<template>
  <el-dialog
      :title="'订单'"
      :close-on-click-modal="false"
      width="30%"
      :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px" :hide-required-asterisk="true">
      <el-form-item label="秒杀id" prop="name">
        <el-input readonly v-model="dataForm.id"></el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input readonly v-model="dataForm.productName"></el-input>
      </el-form-item>
      <el-form-item label="秒杀价格" prop="price">
        <el-input readonly v-model="dataForm.price"></el-input>
      </el-form-item>
      <el-form-item label="剩余数量" prop="seckillCount">
        <el-input readonly v-model="dataForm.seckillCount"></el-input>
      </el-form-item>
      <el-form-item label="支付积分" prop="payPoint">
        <el-input readonly v-model="dataForm.payPoint"></el-input>
      </el-form-item>
      <el-form-item label="购买数量">
        <el-input-number v-model="dataForm.productNum" :min="0"
                         :max="dataForm.seckillLimit>dataForm.seckillCount?dataForm.seckillCount:dataForm.seckillLimit"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button style="margin-left: -60px" @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        key: 1,
        id: 0,
        productId:0,
        productName: '',
        price: '',
        seckillCount: 0,
        payPoint: '',
        seckillLimit: 0,
        productNum: 0,
        
      },
      dataRule: {

        num: [
          {required: true, message: '不能为零', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    handleChange(value) {
      console.log(value);
    },
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          request.get(`/seckill/relation/user/init/${this.dataForm.id}`, {}).then((response) => {
            if (response.data && response.data.code === 200) {
              console.log(response.data)
              this.dataForm.id = response.data.info.id
              this.dataForm.productName = response.data.info.productName
              this.dataForm.productId = response.data.info.productId
              this.dataForm.seckillCount = response.data.info.seckillCount
              this.dataForm.seckillLimit = response.data.info.seckillLimit
              this.dataForm.price = response.data.info.seckillPrice
              this.dataForm.payPoint = response.data.info.payPoint
              this.dataForm.productNum = 0
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid&&this.dataForm.productNum>0) {

          request.post('/order/user/createOrder', {
            'productId': this.dataForm.productId || undefined,
            'productNum': this.dataForm.productNum,
            'price': this.dataForm.price,
            'key': this.dataForm.key,
            'seckillId': this.dataForm.id
          }).then(response => {
            if (response.data && response.data.code === 200) {
              this.visible = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
